import { INDEX, renderSource, setHrefUrlTool } from '@/utils';
import type { IColumnsOption } from 'vc';

export function useTable(data, source) {
  const columns = reactive<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '数据年度',
      field: 'statisticalYear',
      width: 110,
      fixed: 'left',
      align: 'center',
      sortable: true
    },
    {
      title: '报告期',
      field: 'reportingPeriodTypeCode',
      width: 110,
      fixed: 'left',
      align: 'center',
      sortable: true
    },
    {
      title: '支持的项目数量(个)',
      field: 'numberOfSupportedProjects',
      align: 'right',
      sortable: true
    },
    {
      title: '贷款金额(亿元)',
      field: 'loanAmount',
      align: 'right',
      sortable: true
    },
    {
      title: '加权平均率(%)',
      field: 'interestRate',
      align: 'right',
      sortable: true
    },
    {
      title: '带动年度碳减排量(万吨CO2e)',
      field: 'annualCERs',
      align: 'right',
      sortable: true
    }
  ]);

  columns.forEach((item, index) => {
    const _index = index - 1;

    if (index) {
      item.slots = {
        default: ({ row }) => {
          const _value = row?.values[_index]?.indicatorDisplayValue;
          const showJump = row?.values[_index]?.fileUrl && source.value;

          return renderSource(row, _index, showJump, goSource);
        }
      };
    }
  });

  // 溯源弹窗
  const showActionsheet = ref(false);
  // 溯源弹窗数据
  const actionData = ref<Record<string, any>>({});

  const goSource = (data) => {
    setHrefUrlTool(data.fileType, `${data.fileUrl}#page=${data.locationInDocument ? data.locationInDocument.split('P')[1] : ''}`);
  };

  return {
    columns,
    showActionsheet,
    actionData
  };
}
